/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityAffiliationInfo;
import org.kuali.rice.kns.util.TypedArrayList;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityAffiliationsInfoMapper implements ContextMapper {

  private final Log log = LogFactory.getLog(getClass());
  private String affiliationAttributeName = null;
  private String defaultAffiliationAttributeName = null;
  private String campusCodeAttributeName = null;

//beg: methods required by ContextMapper interface
  public List<KimEntityAffiliationInfo> mapFromContext(Object ctx) {
    validateConfiguration();
    List<KimEntityAffiliationInfo> keaiList = new TypedArrayList(KimEntityAffiliationInfo.class);
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;
      String[] affiliations = context.getStringAttributes(affiliationAttributeName);
      String defaultAffiliation = LdapUtilities.materialize(context.getStringAttribute(defaultAffiliationAttributeName));
      String campusCode = LdapUtilities.materialize(context.getStringAttribute(campusCodeAttributeName));
      int id = 0;
      for (String a : affiliations) {
        KimEntityAffiliationInfo keai = new KimEntityAffiliationInfo();
        // the entityAffiliationId is a synthetic identifier for the instance of an entity affiliation.
        // we will generate this based on read position.  this will not likely return a consistent key.
        // this will not maitain referencial integrity, so it should not be used as a system key.
        keai.setEntityAffiliationId(Integer.toString(id));
        keai.setAffiliationTypeCode(a);
        keai.setCampusCode(campusCode);
        keai.setActive(true);//is there an ldap attribute for this?
        keai.setDefault(false);
        if (a.equalsIgnoreCase(defaultAffiliation)) {
          keai.setDefault(true);
        }
        keaiList.add(keai);
        id++;
        if (log.isDebugEnabled()) {
          log.debug("mapped LdapKimEntityAffiliation:"
                  + "(affiliationTypeCode=" + keai.getAffiliationTypeCode() + ")");
        }
      }
    } catch (Throwable t) {
      log.error("Error in ContextMapper", t);
      String msg = "ContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    return keaiList;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getAffiliationAttributeName() {
    return this.affiliationAttributeName;
  }

  public void setAffiliationAttributeName(String attrName) {
    this.affiliationAttributeName = attrName;
  }

  public String getDefaultAffiliationAttributeName() {
    return this.defaultAffiliationAttributeName;
  }

  public void setDefaultAffiliationAttributeName(String attrName) {
    this.defaultAffiliationAttributeName = attrName;
  }

  public String getCampusCodeAttributeName() {
    return this.campusCodeAttributeName;
  }

  public void setCampusCodeAttributeName(String attrName) {
    this.campusCodeAttributeName = attrName;
  }

  private void validateConfiguration() {
    if (affiliationAttributeName == null) {
      String msg = "The property 'affiliationAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (defaultAffiliationAttributeName == null) {
      String msg = "The property 'defaultAffiliationAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (campusCodeAttributeName == null) {
      String msg = "The property 'campusCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods
}
